package com.bitoon.basketdudes.web.jpa;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.apache.log4j.Logger;

/**
 *
 * @author Alberto Piquer
 */
@Entity
@Table(name = "adm_usuario", schema = "central")
@NamedQueries({
    @NamedQuery(name = "AdmUsuario.findAll", query = "SELECT a FROM AdmUsuario a"),
    @NamedQuery(name = "AdmUsuario.findAllOrderById", query = "SELECT a FROM AdmUsuario a order by a.id DESC"),
    @NamedQuery(name = "AdmUsuario.findAllOrderByNick", query = "SELECT a FROM AdmUsuario a order by a.nick ASC"),
    @NamedQuery(name = "AdmUsuario.findById", query = "SELECT a FROM AdmUsuario a WHERE a.id = :id"),
    @NamedQuery(name = "AdmUsuario.findByNick", query = "SELECT a FROM AdmUsuario a WHERE a.nick = :nick"),
    @NamedQuery(name = "AdmUsuario.findByEmail", query = "SELECT a FROM AdmUsuario a WHERE a.email = :email"),
    @NamedQuery(name = "AdmUsuario.findByPassword", query = "SELECT a FROM AdmUsuario a WHERE a.password = :password"),
    @NamedQuery(name = "AdmUsuario.findByFAlta", query = "SELECT a FROM AdmUsuario a WHERE a.fAlta = :fAlta"),
    @NamedQuery(name = "AdmUsuario.findByFUltimo", query = "SELECT a FROM AdmUsuario a WHERE a.fUltimo = :fUltimo"),
    @NamedQuery(name = "AdmUsuario.findByLogin", query = "SELECT a FROM AdmUsuario a WHERE a.nick = :nick and a.password = :password"),
    @NamedQuery(name = "AdmUsuario.findByLenguaje", query = "SELECT a FROM AdmUsuario a WHERE a.lenguaje = :lenguaje")})

 public class AdmUsuario implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "nick")
    private String nick;
    @Column(name = "email")
    private String email;
    @Column(name = "password")
    private String password;
    @Basic(optional = false)
    @Column(name = "f_alta")
    @Temporal(TemporalType.DATE)
    private Date fAlta;
    @Basic(optional = false)
    @Column(name = "f_ultimo")
    @Temporal(TemporalType.DATE)
    private Date fUltimo;
    @Basic(optional = false)
    @Column(name = "lenguaje")
    private String lenguaje;

    @ManyToOne
    @JoinColumn(name = "rol_id", referencedColumnName = "id")
    private AdmRol rolId;

    @Transient
    private Logger log = Logger.getLogger(AdmUsuario.class);

    public AdmUsuario() {
    }

    public AdmUsuario(Integer id) {
        this.id = id;
    }

    public AdmUsuario(Integer id, Date fAlta, Date fUltimo, String lenguaje) {
        this.id = id;
        this.fAlta = fAlta;
        this.fUltimo = fUltimo;
        this.lenguaje = lenguaje;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getFAlta() {
        return fAlta;
    }

    public void setFAlta(Date fAlta) {
        this.fAlta = fAlta;
    }

    public Date getFUltimo() {
        return fUltimo;
    }

    public void setFUltimo(Date fUltimo) {
        this.fUltimo = fUltimo;
    }

    public String getLenguaje() {
        return lenguaje;
    }

    public void setLenguaje(String lenguaje) {
        this.lenguaje = lenguaje;
    }

    public AdmRol getRolId() {
        return rolId;
    }

    public void setRolId(AdmRol rolId) {
        this.rolId = rolId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof AdmUsuario)) {
            return false;
        }
        AdmUsuario other = (AdmUsuario) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.bitoon.basketdudes.jpa.AdmUsuario[id=" + id + "]";
    }

    public boolean tienePermisoLectura(int seccion){
        boolean sw=false;
        log.debug("[tienePermisoLectura] " + seccion);
        try{
            if(rolId.getAdmRolSeccionList().get(seccion).getPermiso()>=2){
                sw=true;
            }
        }catch(Exception e){ log.debug("[tienePermisoLectura] error: " + e); }
        log.debug("[tienePermisoLectura] resultado: " + sw);
        return sw;
    }
}
